<?php

if (!defined('BASEPATH'))
	exit('No Direct Access Allowed !');



class penyakit extends CI_Controller
{
	
	function __construct()
	{

		parent::__construct();
		$this->load->model('m_penyakit');

	}


	function index(){
		$cek = $this->session->userdata('logged_in');
		if (empty($cek)) {
			header('location:' . site_url('login'));
		} else {
			$data['data'] = $this->m_penyakit->ambil();
			$this->load->view('global/header');
			$this->load->view('penyakit/index', $data);
			$this->load->view('global/footer');
		}
	}

	function tambah(){
		$cek = $this->session->userdata('logged_in');
		if (empty($cek)) {
			header('location:' . site_url('login'));
		} else {
			$d['tipe'] = 'Tambah';
			$d['kode_penyakit'] = "";
			$d['nama_penyakit'] = "";
			$d['id'] = "";
			$this->load->view('global/header');
			$this->load->view('penyakit/tambah', $d);
			$this->load->view('global/footer');
		}
	}

	function hapus($id='') {
		$cek = $this->session->userdata('logged_in');
		if (empty($cek)) {
			header('location:' . site_url('admin'));
		} else {
			$this->m_penyakit->hapus($id);
			redirect('penyakit');
		}
	}


	function edit($id=''){
		$cek = $this->session->userdata('logged_in');
		if (empty($cek)) {
			header('location:' . site_url('admin'));
		} else {
			$ambil = $this->m_penyakit->ambilbyid($id)->row();
			$d['kode_penyakit'] = $ambil->kd_penyakit;
			$d['nama_penyakit'] = $ambil->penyakit;
			$d['tipe'] = 'Edit';
			$d['id'] = $id;
			$this->load->view('global/header');
			$this->load->view('penyakit/tambah', $d);
			$this->load->view('global/footer');

		}
	}


	function simpan_penyakit(){
		$cek = $this->session->userdata('logged_in');
		if (empty($cek)) {
			header('location:' . site_url('login'));
		} else { 
			$this->form_validation->set_rules('kode', 'Kode Penyakit', 'trim|required');
			$this->form_validation->set_rules('nama', 'Nama Penyakit', 'trim|required');
			if ($this->input->post('submit')) {
				if ($this->form_validation->run() == FALSE) {
					$data['kode_penyakit'] = $this->input->post('kode');
					$data['nama_penyakit'] = $this->input->post('nama');
					$data['id'] = $this->input->post('id');
					$data['tipe'] = $this->input->post('tipe');
					$this->load->view('global/header');
					$this->load->view('penyakit/tambah', $data);
					$this->load->view('global/footer');
				}else{
					if( $this->input->post('tipe')== 'Tambah'){
						$this->m_penyakit->tambah();
						redirect('penyakit/tambah');
					}else{
						$id = $this->input->post('id');
						$this->m_penyakit->update($id);
						redirect('penyakit/edit/'.$id);
					}
				}
			}
		}
	}


	function gejala($param, $id){
		$cek = $this->session->userdata('logged_in');
		if (empty($cek)) {
			header('location:' . site_url('login'));
		} else {
			$data['id'] = $id;
			$data['data'] = $this->m_penyakit->ambil_gejala($id, $param);
			$data['penyakit'] = $this->m_penyakit->get_nama_penyakit($id);
			$data['param'] = $param;
			$this->load->view('global/header');
			$this->load->view('penyakit/index_gejala', $data);
			$this->load->view('global/footer');
			
		}
	}

	function allgejala(){
		$cek = $this->session->userdata('logged_in');
		if (empty($cek)) {
			header('location:' . site_url('login'));
		} else {
			$data['data'] = $this->m_penyakit->ambilsemuagejala();
			$this->load->view('global/header');
			$this->load->view('penyakit/index_allgejala', $data);
			$this->load->view('global/footer');
			
		}
	}


	function tambah_gejala($id=''){
		$cek = $this->session->userdata('logged_in');
		if (empty($cek)) {
			header('location:' . site_url('login'));
		} else {
			$d['tipe'] = 'Tambah';
			$d['kode_gejala'] = "";
			$d['nama_gejala'] = "";
			$d['id_gejala'] = "";
			$this->load->view('global/header');
			$this->load->view('penyakit/tambah_gejala', $d);
			$this->load->view('global/footer');
		}
	}

	function edit_gejala($id_gejala=''){
		$cek = $this->session->userdata('logged_in');
		if (empty($cek)) {
			header('location:' . site_url('admin'));
		} else {
			$ambil = $this->m_penyakit->ambilgejalabyid($id_gejala)->row();
			$d['kode_gejala'] = $ambil->kd_gejala;
			$d['nama_gejala'] = $ambil->gejala;
			$d['tipe'] = 'Edit';
			$d['id_gejala'] = $id_gejala;
			$this->load->view('global/header');
			$this->load->view('penyakit/tambah_gejala', $d);
			$this->load->view('global/footer');

		}
	}


	function simpan_gejala(){
		$cek = $this->session->userdata('logged_in');
		if (empty($cek)) {
			header('location:' . site_url('login'));
		} else { 
			$this->form_validation->set_rules('kode', 'Kode Gejala', 'trim|required');
			$this->form_validation->set_rules('nama', 'Nama Gejala', 'trim|required');
			$id = $this->input->post('id_gejala');
			if ($this->input->post('submit')) {
				if ($this->form_validation->run() == FALSE) {
					$data['kode_gejala'] = $this->input->post('kode');
					$data['nama_gejala'] = $this->input->post('nama');
					$data['id'] = $id;
					$data['tipe'] = $this->input->post('tipe');
					$this->load->view('global/header');
					$this->load->view('penyakit/tambah_gejala', $data);
					$this->load->view('global/footer');
				}else{
					if( $this->input->post('tipe')== 'Tambah'){
						$this->m_penyakit->tambah_gejala();
						redirect('penyakit/tambah_gejala/');
					}else{
						$id_gejala = $this->input->post('id_gejala');
						$this->m_penyakit->update_gejala($id_gejala);
						redirect('penyakit/edit_gejala/'.$id_gejala);
					}
				}
			}
		}
	}


	function hapus_gejala($id_gejala='') {
		$cek = $this->session->userdata('logged_in');
		if (empty($cek)) {
			header('location:' . site_url('admin'));
		} else {
			$this->m_penyakit->hapus_gejala($id_gejala);
			redirect('penyakit/allgejala');
		}
	}


	// function gejala_json(){
	// 	$data = $this->m_penyakit->get_json();
	// 	$json = json_encode($data);
	// 	print_r($json);
	// }


}